SSM整合Redis Cluster(Redis集群)

SSM整合Redis Cluster(Redis集群)

四月 08, 2020 阅读数(请刷新)

1、导入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- Redis客户端:Jedis -->
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>

<!-- Spring整合redis -->
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>

建议就导入这两个版本的依赖,其他可能会版本会造成jar包冲突。

2、redis.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#redis
redis.pool.maxTotal=30
redis.pool.maxIdle=10
redis.pool.numTestsPerEvictionRun=1024
redis.pool.timeBetweenEvictionRunsMillis=30000
redis.pool.minEvictableIdleTimeMillis=1800000
redis.pool.softMinEvictableIdleTimeMillis=10000
redis.pool.maxWaitMillis=1500
redis.pool.testOnBorrow=true
redis.pool.testWhileIdle=true
redis.pool.blockWhenExhausted=false
redis.maxRedirects=3
redis.host1=192.168.110.131
redis.port1=7001
redis.host2=192.168.110.131
redis.port2=7002
redis.host3=192.168.110.131
redis.port3=7003
redis.host4=192.168.110.131
redis.port4=7004
redis.host5=192.168.110.131
redis.port5=7005
redis.host6=192.168.110.131
redis.port6=7006

3、beans-redis.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 使用.properties文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:redis.properties" />
</bean>
<!-- Jedis配置 -->
<bean id="jedisPoolConfig"
class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="30" />
<!-- 最大空闲连接数 -->
<property name="MaxIdle" value="10" />
<!-- 每次释放连接的最大数目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 释放连接的扫描间隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 连接最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testOnBorrow"
value="${redis.pool.testOnBorrow}" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle"
value="${redis.pool.testWhileIdle}" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted"
value="${redis.pool.blockWhenExhausted}" />
</bean>

<!-- 配置Cluster -->
<bean id="redisClusterConfiguration"
class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="3"></property>
<!-- 节点配置 -->
<property name="clusterNodes">
<set>
<bean
class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host1}"></constructor-arg>
<constructor-arg name="port" value="${redis.port1}"></constructor-arg>
</bean>
<bean
class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host2}"></constructor-arg>
<constructor-arg name="port" value="${redis.port2}"></constructor-arg>
</bean>
<bean
class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host3}"></constructor-arg>
<constructor-arg name="port" value="${redis.port3}"></constructor-arg>
</bean>
<bean
class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host4}"></constructor-arg>
<constructor-arg name="port" value="${redis.port4}"></constructor-arg>
</bean>
<bean
class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host5}"></constructor-arg>
<constructor-arg name="port" value="${redis.port5}"></constructor-arg>
</bean>
<bean
class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="${redis.host6}"></constructor-arg>
<constructor-arg name="port" value="${redis.port6}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<!-- 配置jedis连接工厂 -->
<bean id="jeidsConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg ref="redisClusterConfiguration" />
<constructor-arg ref="jedisPoolConfig" />
</bean>
<!-- 配置RedisTemplate -->
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory"
ref="jeidsConnectionFactory" />
<!-- String -->
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
<!-- hash -->
<property name="hashKeySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashValueSerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>
<!-- 定义缓存管理器redisCacheManager。 注意:cache-manager默认值是cacheManager,你的缓存管理器id要是命名是cacheManager,这里可以省略
1.使用注解驱动 -->
<cache:annotation-driven
cache-manager="redisCacheManager" />
<!-- 2.定义缓存管理器 -->
<bean id="redisCacheManager"
class="org.springframework.data.redis.cache.RedisCacheManager">
<!-- 通过构造方法注入redisTemplate -->
<constructor-arg index="0" ref="redisTemplate" />
<!-- 定义超时时间,单位秒 -->
<property name="defaultExpiration" value="5000"></property>
<!-- 设置缓存器名称 -->
<property name="cacheNames">
<list>
<value>redisCacheManager</value>
</list>
</property>
</bean>

</beans>

4、注解使用

在BeanServiceImpl.java文件中对方法进行注解添加
@CachePut注解:
如果缓存需要更新,且不干扰方法的执行,可以使用注解@CachePut。@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。

1
2
3
4
5
6
@Override
@CachePut(value="redisCacheManager",key="'insertP'")
public void insertProduct(Product product,CommonsMultipartFile file, HttpServletRequest request){
service.insertP(product);
}
// key对应的是mapper中的方法,并不是BeanServiceImpl.java方法

@Cacheable注解:
用来声明方法是可缓存的。将结果存储到缓存中以便后续使用相同参数调用时不需执行实际的方法。直接从缓存中取值。最简单的格式需要制定缓存名称。

1
2
3
@Cacheable(value="redisCacheManager",key="'findProduct'")
public List<Product> findProduct(Integer pageNo){
service.findProduct(product);}

@CacheEvict是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。
PS:
value的值是redis配置文件对应的缓存管理器的id值
key=”‘insertProduct’”:是要缓存到redis中的值,下次spring会直接通过key获取结果。注意key的值中还有个单引号。
@Cacheable:适合查询,有返回值的方法上。因为,每次请求在进入方法之前,spring会先从缓存服务器中查找对应的key,如果有,就直接返回。否则再去查询数据库,在将结果通过key保存到缓存中。
@CachePut:适合添加,修改,void方法上。因为spring不会事先去缓存服务器中查询数据,而是直接执行方法,然后将结果通过key保存到缓存中。这样是防止数据不一致。

配置完成后重启服务器,可能会报错

1
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

查看Redis集群踩的坑及报错记录并解决

一、Redis服务器搭建与学习
二、Redis Cluster(Redis 集群)的搭建
四、Redis集群踩的坑及报错记录并解决